{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 三招解决数据连接问题 - 注意事项"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- left 表有重复, 但 right 表无重复\n",
    "- left 表无重复, 但 right 表有重复\n",
    "- left 表有重复, 且 right 表有重复"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 导入工具"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. left 表有重复, 但 right 表无重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "left = pd.DataFrame(\n",
    "    data=[\n",
    "    ['2023-1-1', '001', '报销餐费', -300],\n",
    "    ['2023-1-2', '001', '报销差旅费', -500],\n",
    "    ['2023-1-3', '002', '报销交通费', -80],\n",
    "    ['2023-1-4', '003', '按揭回款', 800000],\n",
    "    ['2023-1-5', '003', '工资', -10000],\n",
    "    ['2023-1-6', '002', '工程款', -1000000]], \n",
    "    columns=['日期', '账号编号', '摘要', '金额']\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>账号编号</th>\n",
       "      <th>摘要</th>\n",
       "      <th>金额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-1-1</td>\n",
       "      <td>001</td>\n",
       "      <td>报销餐费</td>\n",
       "      <td>-300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-1-2</td>\n",
       "      <td>001</td>\n",
       "      <td>报销差旅费</td>\n",
       "      <td>-500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-1-3</td>\n",
       "      <td>002</td>\n",
       "      <td>报销交通费</td>\n",
       "      <td>-80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-1-4</td>\n",
       "      <td>003</td>\n",
       "      <td>按揭回款</td>\n",
       "      <td>800000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-1-5</td>\n",
       "      <td>003</td>\n",
       "      <td>工资</td>\n",
       "      <td>-10000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2023-1-6</td>\n",
       "      <td>002</td>\n",
       "      <td>工程款</td>\n",
       "      <td>-1000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         日期 账号编号     摘要       金额\n",
       "0  2023-1-1  001   报销餐费     -300\n",
       "1  2023-1-2  001  报销差旅费     -500\n",
       "2  2023-1-3  002  报销交通费      -80\n",
       "3  2023-1-4  003   按揭回款   800000\n",
       "4  2023-1-5  003     工资   -10000\n",
       "5  2023-1-6  002    工程款 -1000000"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "right = pd.DataFrame(\n",
    "    {\n",
    "        '账号编号': ['001', '002', '003'],\n",
    "        '开户银行': ['建设银行', '江西银行', '交通银行'],\n",
    "        '网点': ['青云支行', '东湖支行', '赣江支行'],\n",
    "        '账户简称': ['建行青云', '江西东湖', '交行赣江']\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>账号编号</th>\n",
       "      <th>开户银行</th>\n",
       "      <th>网点</th>\n",
       "      <th>账户简称</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>001</td>\n",
       "      <td>建设银行</td>\n",
       "      <td>青云支行</td>\n",
       "      <td>建行青云</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>002</td>\n",
       "      <td>江西银行</td>\n",
       "      <td>东湖支行</td>\n",
       "      <td>江西东湖</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>003</td>\n",
       "      <td>交通银行</td>\n",
       "      <td>赣江支行</td>\n",
       "      <td>交行赣江</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  账号编号  开户银行    网点  账户简称\n",
       "0  001  建设银行  青云支行  建行青云\n",
       "1  002  江西银行  东湖支行  江西东湖\n",
       "2  003  交通银行  赣江支行  交行赣江"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = pd.merge(\n",
    "    left=left,\n",
    "    right=right[['账号编号', '账户简称']],\n",
    "    on='账号编号',\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='images/左重复右不重.png'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>账号编号</th>\n",
       "      <th>摘要</th>\n",
       "      <th>金额</th>\n",
       "      <th>账户简称</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-1-1</td>\n",
       "      <td>001</td>\n",
       "      <td>报销餐费</td>\n",
       "      <td>-300</td>\n",
       "      <td>建行青云</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-1-2</td>\n",
       "      <td>001</td>\n",
       "      <td>报销差旅费</td>\n",
       "      <td>-500</td>\n",
       "      <td>建行青云</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-1-3</td>\n",
       "      <td>002</td>\n",
       "      <td>报销交通费</td>\n",
       "      <td>-80</td>\n",
       "      <td>江西东湖</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-1-6</td>\n",
       "      <td>002</td>\n",
       "      <td>工程款</td>\n",
       "      <td>-1000000</td>\n",
       "      <td>江西东湖</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-1-4</td>\n",
       "      <td>003</td>\n",
       "      <td>按揭回款</td>\n",
       "      <td>800000</td>\n",
       "      <td>交行赣江</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2023-1-5</td>\n",
       "      <td>003</td>\n",
       "      <td>工资</td>\n",
       "      <td>-10000</td>\n",
       "      <td>交行赣江</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         日期 账号编号     摘要       金额  账户简称\n",
       "0  2023-1-1  001   报销餐费     -300  建行青云\n",
       "1  2023-1-2  001  报销差旅费     -500  建行青云\n",
       "2  2023-1-3  002  报销交通费      -80  江西东湖\n",
       "3  2023-1-6  002    工程款 -1000000  江西东湖\n",
       "4  2023-1-4  003   按揭回款   800000  交行赣江\n",
       "5  2023-1-5  003     工资   -10000  交行赣江"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. left 表无重复, 但 right 表有重复"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "同 2，left 和 right 互换而已。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. left 表有重复，且 right 表有重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取数据的文件路径\n",
    "xlsalecoll = r'data\\sales\\销售收款.xlsx'\n",
    "xlcash = r'data\\account\\出纳日记账.xlsx'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "left = pd.read_excel(xlsalecoll)\n",
    "left = left.groupby('日期').agg({'金额': 'sum'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "right = pd.concat([pd.read_excel(xlcash, sheet_name=i) for i in range(3)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "right = right[lambda x: x.摘要.str.contains('售房')].groupby('日期').agg({'借方': 'sum'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = pd.merge(\n",
    "    left=left,\n",
    "    right=right,\n",
    "    on='日期',\n",
    "    how='outer',\n",
    "    sort=True\n",
    ").assign(\n",
    "    差额=lambda x: x.金额.sub(x.借方, fill_value=0).cumsum()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='images/左重复且右重复.png'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>金额</th>\n",
       "      <th>借方</th>\n",
       "      <th>差额</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-01</th>\n",
       "      <td>20000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-02</th>\n",
       "      <td>NaN</td>\n",
       "      <td>20000.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-05</th>\n",
       "      <td>180000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-06</th>\n",
       "      <td>NaN</td>\n",
       "      <td>180000.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-08</th>\n",
       "      <td>800000.0</td>\n",
       "      <td>800000.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-15</th>\n",
       "      <td>900000.0</td>\n",
       "      <td>900000.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-20</th>\n",
       "      <td>50000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>50000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-21</th>\n",
       "      <td>NaN</td>\n",
       "      <td>50000.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-25</th>\n",
       "      <td>150000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>150000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-26</th>\n",
       "      <td>NaN</td>\n",
       "      <td>150000.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-31</th>\n",
       "      <td>880000.0</td>\n",
       "      <td>850000.0</td>\n",
       "      <td>30000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  金额        借方        差额\n",
       "日期                                      \n",
       "2023-01-01   20000.0       NaN   20000.0\n",
       "2023-01-02       NaN   20000.0       0.0\n",
       "2023-01-05  180000.0       NaN  180000.0\n",
       "2023-01-06       NaN  180000.0       0.0\n",
       "2023-01-08  800000.0  800000.0       0.0\n",
       "2023-01-15  900000.0  900000.0       0.0\n",
       "2023-01-20   50000.0       NaN   50000.0\n",
       "2023-01-21       NaN   50000.0       0.0\n",
       "2023-01-25  150000.0       NaN  150000.0\n",
       "2023-01-26       NaN  150000.0       0.0\n",
       "2023-01-31  880000.0  850000.0   30000.0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
